package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.guild.R;
import com.wude.guild.servercore.app.CubeFragment;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 忘记密码 输入密码  新密码
 * Created by Administrator on 2017/3/28.
 */
@SuppressLint("HandlerLeak")
public class ForgetPassTweFragment extends CubeFragment implements View.OnClickListener {
    TextView yanzheng;
    EditText phone;
    EditText yanzhengma;
    private TextView ok;

    private boolean niu = true;//是否是第二次获取验证码
    private String onePass;
    private CountDownTimer timer;//验证码倒计时

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_forgetpasstwe, null);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("ForgetPassTweFragment");
    }

    @Override
    public void onPause() {
        super.onPause();

        MobclickAgent.onPageEnd("ForgetPassTweFragment");
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        yanzheng = findView(view, R.id.yanzheng);
        phone = findView(view, R.id.phone);
        yanzhengma = findView(view, R.id.yanzhengma);
        ok = findView(view, R.id.ok);

        ok.setOnClickListener(this);
        yanzheng.setOnClickListener(this);

        timer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long l) {
                yanzheng.setClickable(false);
                yanzheng.setText(l / 1000 + "秒后重试");
                niu = false;
            }

            @Override
            public void onFinish() {
                niu = false;
                yanzheng.setText("重新发送");
                yanzheng.setClickable(true);
            }
        };
    }

    @Override
    public void onEnter(Object data) {
        super.onEnter(data);
        onePass = (String) data;
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.yanzheng:
                getYanzheng();
                break;
            case R.id.ok:
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(), 0);
                }
                String s = phone.getText().toString();
                String s1 = yanzhengma.getText().toString();
                if (s.equals("")) {
                    ToastUtil.showToast("请输入账号");
                    return;
                }
                if (s1.equals("")) {
                    ToastUtil.showToast("请输入验证码");
                    return;
                }
                HashMap<String, String> map = new HashMap<>();
                map.put("account", s);
                map.put("v_code", s1);
                map.put("password", onePass);
                HttpCom.POST(vhandler, HttpCom.getUserForgetPassURL(), map, false);
                break;
        }
    }

    public void getYanzheng() {
        String s = phone.getText().toString();
        if (s.equals("")) {
            ToastUtil.showToast("请输入账号");
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("account", s);
//        map.put("type", niu ? "1" : "2");
        HttpCom.POST(handler, HttpCom.getUserForgetPassmsgURL(), map, false);
    }

    /**
     * 获取验证码
     */
    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Log.e("发送验证码json", msg.obj.toString());
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        int status = jsonObject.getInt("status");
                        String return_code = jsonObject.getString("return_code");
                        ToastUtil.showToast(return_code);
                        if (status == 1) {
                            timer.start();
                        } else {
                            niu = true;
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.e("解析验证码异常", e.toString());
                    }
                    break;
                case 2:
                    niu = true;
                    break;
            }
        }
    };

    /**
     * 修改新密码
     */
    Handler vhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    boolean a = HttpUtils.DNSForget(msg.obj.toString());
                    if (a) {
                        getActivity().finish();
                    }
                    break;
                case 2:

                    break;
            }
        }
    };

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_forgetpass, null);
    }
}
